/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package graphdisplay;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import javax.swing.JPanel;

/**
 *
 * @author Jimmy
 */
class Node extends JPanel implements Displayable, DragSourceListener, DragGestureListener {

    static int idCounter = 0;
    int id;
    private int x, y;
    private String name;
    int xOffset = 8;
    int yOffset = 8;
    DragSource ds = DragSource.getDefaultDragSource();

    Node(String name, int x, int y) {

        this.name = name;
        this.x = x;
        this.y = y;
        setBounds(x, y, 16, 16);
        setMaximumSize(new Dimension(16, 16));
        setMinimumSize(new Dimension(16, 16));
        ds.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
    }
    
    public void setId(int id){
        this.id = id;
    }
    
    public int getId(){
        return id;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public String getName() {
        return name;
    }
    
    public void setName(String name){
        this.name = name;
    }

    public boolean equals(Object o) {
        try{
            return ((Node) o).name.equals(this.name);
        }catch(java.lang.ClassCastException e){
            System.out.println("class exception");
            System.out.println(e);            
        }
        return false;        
    }

    @Override
    protected void paintComponent(java.awt.Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.black);
        g.fillOval(0, 0, 16, 16);
        //g.drawString(name, 0, 0);
    }

    public void dragGestureRecognized(DragGestureEvent arg0) {
        Transferable t = new StringSelection("");
        arg0.startDrag(DragSource.DefaultMoveDrop, t, this);
    }

    public void dragDropEnd(DragSourceDropEvent arg0) {
    }

    public void dragEnter(DragSourceDragEvent arg0) {
    }

    public void dragExit(DragSourceEvent arg0) {
    }

    public void dragOver(DragSourceDragEvent arg0) {

        setX(Constant.x);
        setY(Constant.y);
        setBounds(getX(), getY(), 16, 16);
        repaint();
    }

    public void dropActionChanged(DragSourceDragEvent arg0) {
    }
}

class Constant {

    public static int x;
    public static int y;
}